<?php

namespace app\common\model;

use think\Db;
use think\Model;
class
News extends Model
{
    public static function get_class_list()
    {
        $where = "status = '1'";
        $field = "id,name";
        $order = "weigh DESC,add_time DESC";
        $list = Db::name("newsclass")->where($where)->field($field)->order($order)->select();
        return $list;
    }

    public static function get_news_list($page,$class_id)
    {
        $where = "class_id = {$class_id}";
        $where.= " AND status = '1'";
        $field = "id,title,images,viewcount,add_time";
        $order = "weigh DESC,add_time DESC";
        $count = self::where($where)->count();
        $limit = 20;
        $start = ($page - 1) * $limit;
        $total = ceil($count / $limit);
        $list = self::where($where)->field($field)->order($order)->limit($start, $limit)->select();
        $data = [
            'total'=>$total,
            'list'=>$list
        ];
        return $data;
    }

    public static function get_news_info($id)
    {
        $where = "id = {$id}";
        $info = self::where($where)->field("*")->find();
        if($info && !empty($info["content"])){
            $info["content"]=get_image_url($info["content"],protocol_name() . '://' . $_SERVER['SERVER_NAME']);
        }
        self::where($where)->setInc('viewcount',1);
        return $info;
    }

    public function getAddTimeAttr($value)
    {
        if($value){
            $current_year = date('Y',time());
            $year = date('Y',$value);
            if($current_year != $year){
                return date('Y年m月d日',$value);
            }else{
                return date('m月d日',$value);
            }
        }else{
            return "";
        }
    }

    public function getImagesAttr($value)
    {
        if($value){
            $images_arr = explode(',',$value);
            foreach ($images_arr as $key=>&$value){
                $value = splicing_domain($value);
            }
            return $images_arr;
        }else{
            return [];
        }
    }
}
